﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Toolkit
{
    /// <summary>
    /// 表格的分页工具
    /// </summary>
    public partial class BindingDataGridPager : Control
    {
        public BindingDataGridPager()
        {
            InitializeComponent();
          
        }

       

        private bool busy = false;

        protected ButtonBase FirstPageButton;

        protected ButtonBase PreviousPageButton;

        protected ButtonBase NextPageButton;

        protected ButtonBase LastPageButton;

        protected TextBlock DisplayTextBlock;

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            DisplayTextBlock = (TextBlock)this.GetTemplateChild("DisplayTextBlock");
            FirstPageButton = (ButtonBase)this.GetTemplateChild("FirstPageButton");
            PreviousPageButton= (ButtonBase)this.GetTemplateChild("PreviousPageButton");
            NextPageButton= (ButtonBase)this.GetTemplateChild("NextPageButton");
            LastPageButton = (ButtonBase)this.GetTemplateChild("LastPageButton");
            InitClick(FirstPageButton,FirstPageClick);
            InitClick(PreviousPageButton, PreviousPageClick);
            InitClick(NextPageButton, NextPageClick);
            InitClick(LastPageButton,LastPageClick);
            GoToPage(1);
        }

        public ICommand PageChangedCommand
        {
            get { return (ICommand)GetValue(PageChangedCommandProperty); }
            set { SetValue(PageChangedCommandProperty, value); }
        }

        public static readonly DependencyProperty PageChangedCommandProperty =
            DependencyProperty.Register("PageChangedCommand", typeof(ICommand), typeof(BindingDataGridPager), new PropertyMetadata(null));

        public void ReLoad()
        {
            GoToPage(CurrentPage);
        }

        public void GoToPage(int? pageIndex)
        {
            if (busy)
            {
                return;
            }
            if (this.TotalPage > -1 && pageIndex > this.TotalPage) 
            {
                pageIndex = TotalPage;
            }
            if (pageIndex < 1 || pageIndex == null)
            {
                pageIndex = 1;
            }
            busy = true;
            if (PageChangedCommand != null && PageChangedCommand.CanExecute(pageIndex))
            {
                PageChangedCommand.Execute(pageIndex);
            }
            busy = false;
        }

        void InitClick(ButtonBase button,RoutedEventHandler de)
        {
            if (button != null)
            {
                button.Click += de;
            }
        }

        void ChangeButtonVisibility(ButtonBase button, bool visible)
        {
            if (button != null)
            {
                if (visible)
                 button.Visibility = System.Windows.Visibility.Visible;
                else
                 button.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        void ChangeButtonEnable(ButtonBase button, bool IsEnabled)
        {
            if (button != null)
            {
                button.IsEnabled = IsEnabled;
            }
        }

        public virtual void RefreshDisplay()
        {
            if (DisplayTextBlock != null)
            DisplayTextBlock.Text = string.Format(DisplayFormat,CurrentPage,TotalPage,TotalCount,PageSize);// CurrentPage + "/" + TotalPage;

            ChangeButtonEnable(FirstPageButton, true);
            ChangeButtonEnable(PreviousPageButton, true);
            ChangeButtonEnable(NextPageButton, true);
            ChangeButtonEnable(LastPageButton, true);

            if (CurrentPage <= 1)
            {
                ChangeButtonEnable(FirstPageButton, false);
                ChangeButtonEnable(PreviousPageButton, false);
            }
            if (CurrentPage >= TotalPage)
            {
                ChangeButtonEnable(NextPageButton, false);
                ChangeButtonEnable(LastPageButton, false);
            }
        }


        private void FirstPageClick(object sender, RoutedEventArgs e)
        {
            GoToPage(1);
        }
        private void PreviousPageClick(object sender, RoutedEventArgs e)
        {
            GoToPage(CurrentPage - 1);
        }
        private void NextPageClick(object sender, RoutedEventArgs e)
        {
            GoToPage(CurrentPage+1);
        }
        private void LastPageClick(object sender, RoutedEventArgs e)
        {
            GoToPage(TotalPage);
        }


        /// <summary>
        /// 总页数
        /// </summary>
        public int? TotalPage
        {
            get { return (int?)GetValue(TotalPageProperty); }
            set { if (value > -1) SetValue(TotalPageProperty, value); }
        }

        /// <summary>  
        /// 总记录数  
        /// </summary>
        public int? TotalCount
        {
            get { return (int?)GetValue(TotalCountProperty); }
            set { if (value > -1) SetValue(TotalCountProperty, value); }
        }

        /// <summary>  
        /// 每页记录数
        /// </summary>  
        public int? PageSize
        {
            get { return (int?)GetValue(PageSizeProperty); }
            set { if (value > 0) SetValue(PageSizeProperty, value); }
        }

        /// <summary>  
        /// 当前页码
        /// </summary> 
        public int? CurrentPage
        {
            get { return (int?)GetValue(CurrentPageProperty); }
            set { if (value > 0) SetValue(CurrentPageProperty, value); }
        }

        public string DisplayFormat
        {
            get { return (string)GetValue(DisplayFormatProperty); }
            set { SetValue(DisplayFormatProperty, value); }
        }


        public static readonly DependencyProperty TotalPageProperty =
          DependencyProperty.Register("TotalPage", typeof(int?), typeof(BindingDataGridPager), new PropertyMetadata(-1, (a, v) => {
              BindingDataGridPager dp = (BindingDataGridPager)a;
              dp.RefreshDisplay();
          }));

        public static readonly DependencyProperty TotalCountProperty =
            DependencyProperty.Register("TotalCount", typeof(int?), typeof(BindingDataGridPager), new PropertyMetadata(-1, (a, v) => {
                BindingDataGridPager dp = (BindingDataGridPager)a;
                dp.RefreshDisplay();
            }));

        public static readonly DependencyProperty PageSizeProperty =
            DependencyProperty.Register("PageSize", typeof(int?), typeof(BindingDataGridPager),new PropertyMetadata(-1, (a, v) => {
                BindingDataGridPager dp = (BindingDataGridPager)a;
                dp.RefreshDisplay();
            }));
       
        public static readonly DependencyProperty CurrentPageProperty =
            DependencyProperty.Register("CurrentPage", typeof(int?), typeof(BindingDataGridPager),new PropertyMetadata(-1, (a, v) => {
                BindingDataGridPager dp = (BindingDataGridPager)a;
                dp.RefreshDisplay();
            }));


        public static readonly DependencyProperty DisplayFormatProperty =
         DependencyProperty.Register("DisplayFormat", typeof(string), typeof(BindingDataGridPager), new PropertyMetadata("{0}/{1}", (a, v) =>{
             BindingDataGridPager dp = (BindingDataGridPager)a;
             dp.RefreshDisplay();
         }));
  
    }
 
}